import 'package:flutter/material.dart';
import 'package:collins_vocabulary/model/word.dart';
import 'package:collins_vocabulary/common/phmp3.dart';
import 'package:collins_vocabulary/common/detail_means.dart';
import 'package:shared_preferences/shared_preferences.dart';

class WordDetail extends StatefulWidget {
  final Word item;
  final SharedPreferences prefs;
  WordDetail({Key key, this.item, this.prefs}) : super(key: key);

  @override
  WordDetailState createState() => WordDetailState();
}

class WordDetailState extends State<WordDetail> {
  bool showCn;
  bool showCollins;
  bool showSentence;

  @override
  void initState() {
    super.initState();
    setState(() {
      showCn = widget.prefs.getBool('showCn');
      showCollins = widget.prefs.getBool('showcollins');
      showSentence = widget.prefs.getBool('sentence');
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  Widget _getPhMp3() {
    if (widget.prefs.getBool('en_ph')) {
      if (widget.item.ph_en_mp3.toString().isEmpty) {
        return Text('无法获取发音');
      }
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          PhMp3(
            text: widget.item.ph_en,
            src: widget.item.ph_en_mp3,
            color: Colors.blueGrey,
            autoplay: false,
          ),
          Text(
            '（美式发音）',
            style: TextStyle(
              color: Color(0xFFffc75f),
              fontSize: 12.0,
              fontWeight: FontWeight.bold,
            ),
          )
        ],
      );
    } else {
      if (widget.item.ph_am_mp3.toString().isEmpty) {
        return Text('无法获取发音');
      }
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          PhMp3(
            text: widget.item.ph_am,
            src: widget.item.ph_am_mp3,
            color: Colors.blueGrey,
            autoplay: false,
          ),
          Text(
            '（美式发音）',
            style: TextStyle(
              color: Color(0xFFFF5FAA),
              fontSize: 12.0,
              fontWeight: FontWeight.bold,
            ),
          )
        ],
      );
    }
  }

  Widget _generateWidget() {
    final word = widget.item;
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Container(
            padding: const EdgeInsets.symmetric(vertical: 10.0),
            child: Text(
              word.text,
              style: TextStyle(
                fontSize: 40,
                fontWeight: FontWeight.bold,
                color: Color(0xFF7F8AEE),
              ),
            ),
          ),
          _getPhMp3(),
          Padding(padding: const EdgeInsets.only(top: 10.0)),
          DetailMeans(
            word: word,
            showCn: showCn,
            showCollins: showCollins,
            showSentence: showSentence,
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        title: Text(
          '“${widget.item.text}”单词详情',
          style: TextStyle(
            color: Color(0xFF7F8AEE),
            fontSize: 14,
          ),
        ),
        iconTheme: IconThemeData(color: Color(0xFF7F8AEE)),
        backgroundColor: Colors.white,
      ),
      body: _generateWidget(),
    );
  }
}
